#!/bin/bash

INSTALL_PREFIX=/home/${USER}/tmp/bin/llvm-6.0.0
mkdir -p ${INSTALL_PREFIX}

wget http://llvm.org/releases/6.0.0/llvm-6.0.0.src.tar.xz
wget http://llvm.org/releases/6.0.0/cfe-6.0.0.src.tar.xz
wget http://llvm.org/releases/6.0.0/compiler-rt-6.0.0.src.tar.xz

tar -xf llvm-6.0.0.src.tar.xz &&
mv llvm-6.0.0.src llvm &&
cd llvm &&
tar -xf ../cfe-6.0.0.src.tar.xz -C tools &&
tar -xf ../compiler-rt-6.0.0.src.tar.xz -C projects &&

mv tools/cfe-6.0.0.src tools/clang &&
mv projects/compiler-rt-6.0.0.src projects/compiler-rt &&

mkdir -v build &&
cd       build &&

CC=gcc CXX=g++					\
cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}	\
	-DLLVM_ENABLE_FFI=ON			\
	-DCMAKE_BUILD_TYPE=Release		\
	-DLLVM_BUILD_LLVM_DYLIB=ON		\
	-DLLVM_LINK_LLVM_DYLIB=ON		\
	-DLLVM_TARGETS_TO_BUILD="host"		\
	-DLLVM_BUILD_TESTS=ON			\
	-Wno-dev -G Ninja ..			&&
ninja
sudo ninja install
